#include "logindialog.h"
#include "ui_logindialog.h"



LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
}

LoginDialog::~LoginDialog()
{
    delete ui;
}

void LoginDialog::InitSlots(bool b)
{
    connect(ClientApplication::Instance(),SIGNAL(authorization()),this,SLOT(ProceedAuthorization()),Qt::DirectConnection);
    connect(ClientApplication::Instance(),SIGNAL(registration()),this,SLOT(ProceedRegistration()),Qt::DirectConnection);
    connect(ClientApplication::Instance(),SIGNAL(fail(QString)),this,SLOT(Failed(QString)),Qt::DirectConnection);
}

void LoginDialog::DestroySlots()
{
    disconnect(this,0);
}

void LoginDialog::MakeConnection(QString IP, QString Port, bool b)
{
    ClientApplication::CreateClient(new Session(),IP ,Port);
    if (ClientApplication::Instance()->getStarted())
        ClientApplication::Instance()->StopClient();
    DestroySlots();
    InitSlots(b);
    ClientApplication::Instance()->StartClient();
}

void LoginDialog::on_pBLogin_clicked()
{
    QRegExp pass = QRegExp("^[0-9a-zA-Z#_-&%]+$");
    if (pass.exactMatch(this->ui->lEPassword->text()))
    {
        MakeConnection(this->ui->lEIp->text(), this->ui->lEPort->text(),true);
        if (ClientApplication::Instance()->getConnected())
        {
            QMessageBox msgBox;
            msgBox.setText("Connected");
            msgBox.exec();
            QueryManager::SendAuthorization(this->ui->lELogin->text(),this->ui->lEPassword->text());
        }
        else
        {
            QMessageBox msgBox;
            msgBox.setText("Unable to connect to remote host");
            msgBox.exec();
        }
    }
    else{
        QMessageBox msgBox;
        msgBox.setText("Wrong password symbol");
        msgBox.exec();
    }
}

void LoginDialog::on_pBRegister_clicked()
{
    QRegExp pass = QRegExp("^[0-9a-zA-Z#_-&%]{4,255}$");
    if (pass.exactMatch(this->ui->lEPassword->text()))
    {
        MakeConnection(this->ui->lEIp->text(), this->ui->lEPort->text(),false);
        if (ClientApplication::Instance()->getConnected())
        {
            QMessageBox msgBox;
            msgBox.setText("Connected");
            msgBox.exec();
            QueryManager::SendRegistration(this->ui->lELogin->text(),this->ui->lEPassword->text());
        }
        else
        {
            QMessageBox msgBox;
            msgBox.setText("Unable to connect");
            msgBox.exec();
        }
    }
    else{
        QMessageBox msgBox;
        msgBox.setText("Wrong password symbol,\nor password is too small");
        msgBox.exec();
    }
}

void LoginDialog::ProceedAuthorization()
{
    disconnect(this,0);
    this->child = new MainWindow(this);
    this->child->show();
    this->hide();
}

void LoginDialog::ProceedRegistration()
{
    QueryManager::SendAuthorization(this->ui->lELogin->text(),this->ui->lEPassword->text());
}

void LoginDialog::Failed(QString cause)
{
    QMessageBox msgBox;
    msgBox.setText(ErrorDict::getMessage(cause));
    msgBox.setModal(true);
    msgBox.exec();
}
